<template>
  <div>
    <loginheader>联合登录</loginheader>
    <section class="container">
      <nav class="tab">
        <a
          @click="hasAccount = true"
          :class="{ active: hasAccount }"
          href="javascript:;"
        >
          <i class="iconfont icon-bind" />
          <span>已有小兔鲜账号，请绑定手机</span>
        </a>
        <a
          @click="hasAccount = false"
          :class="{ active: !hasAccount }"
          href="javascript:;"
        >
          <i class="iconfont icon-edit" />
          <span>没有小兔鲜账号，请完善资料</span>
        </a>
      </nav>
      <div class="tab-content" v-if="hasAccount">
        <loginBind
          :nowname="nowname"
          :headimg="headimg"
          :openid="openid"
          @change="changeAccout"
        ></loginBind>
      </div>
      <div class="tab-content" v-else>
        <loginpatch></loginpatch>
      </div>
    </section>
    <loginfooter :openid="openid"></loginfooter>
  </div>
</template>

<script>
import loginheader from "@/components/login/LoginHeader.vue";
import loginBind from "./loginBind.vue";
import loginfooter from "@/components/login/loginfooter.vue";
import loginpatch from "@/components/login/loginpatch.vue";
import { qqlogin } from "@/utils/Api/NavText";
import qc from "qc";
//去首页去调用后台的接口 然后检查我当前的账号是否被注册过并且绑定过 ，如果注册并被绑定  那么储存完用户的信息后直接跳转到首页
//注册过但没有绑定 提示用户绑定手机号  没有注册过 完善信息（注册）
export default {
  components: { loginheader, loginfooter, loginBind, loginpatch },
  data() {
    return {
      hasAccount: true, //是否有账号
      nowname: "",
      headimg: "",
      openid: "", //登录的唯一标识
    };
  },
  mounted() {
    this.checkqq();
  },
  methods: {
    checkqq() {
      //qc.Login.check() //检查你是否同意qq已经登录 true qq已经登录 false是没有登陆
      if (qc.Login.check()) {
        //qq已经登录需要检查账号是否已经被注册过
        qc.Login.getMe((openid) => {
          // console.log(openid);
          this.openid = openid;
          //去调用api接口检查openid 是否在本网站已经被注册过
          qqlogin(openid)
            .then((res) => {
              console.log(111);
              this.$msg({ text: "登录成功", type: "success" });
              // console.log(res);
              let { id, account, avatar, nickname, token } = res.result;
              this.$store.commit("user/setUser", {
                id,
                account,
                avatar,
                nickname,
                token,
              });
              this.$router.push('/')
            })
            .catch((err) => {
              console.log(222);
              //第三方登录失败
            });
          //获取到当前用户的信息 展示在页面上
          qc.api("get_user_info").success((res) => {
            this.nowname = res.data.nickname;
            this.headimg = res.data.figureurl;
          });
        });
      }
    },
    //切换选项啦
    changeAccout() {
      this.hasAccount = !this.hasAccount;
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/css/color.scss";
.container {
  padding: 25px 0;
}
.tab {
  background: #fff;
  height: 80px;
  padding-top: 40px;
  font-size: 18px;
  text-align: center;
  a {
    color: #666;
    display: inline-block;
    width: 350px;
    line-height: 40px;
    border-bottom: 2px solid #e4e4e4;
    i {
      font-size: 22px;
      vertical-align: middle;
    }
    span {
      vertical-align: middle;
      margin-left: 4px;
    }
    &.active {
      color: $xtxColor;
      border-color: $xtxColor;
    }
  }
}
.tab-content {
  min-height: 600px;
  background: #fff;
}
</style>
